package com.go8.mall.controller;

import com.go8.search.pojo.Product;
import com.go8.search.pojo.SearchedData;
import com.go8.search.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class SearchController {
    @Autowired
    private SearchService searchService;

    @GetMapping("/range.html")
    public String rage(String q,String range, @RequestParam(defaultValue = "1") int page,
                       @RequestParam(defaultValue = "60") int size, Model model) {
        String[] split = range.split("-");
        int low = new Integer(split[0]).intValue()*100;
        int high = new Integer(split[1]).intValue()*100;
        SearchedData<Product> sd = searchService.searchByKeywordsAndPriceRange
                (q,low ,high, page, size);
        model.addAttribute("sd", sd);
        /*第二节课*/
        model.addAttribute("q", q);
        return "list";
    }

    @GetMapping("/sort.html")
    public String sort(String q,String order, @RequestParam(defaultValue = "1") int page,
                       @RequestParam(defaultValue = "60") int size, Model model) {
        SearchedData<Product> sd = searchService.searchByKeywordsAndPriceOrder(q, order, page, size);
        model.addAttribute("sd", sd);
        /*第二节课*/
        model.addAttribute("q", q);
        return "list";
    }
}
